﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using SpeakupBackend;
using WebLayer;

namespace UserVerifier
{
    class Program
    {
        static void Main(string[] args)
        {
            Trace.Listeners.Add(new ConsoleTraceListener());

            string inputFile = "users.csv";
            string outputFile = "result.txt";
            
            Console.WriteLine("Input  file: {0}", inputFile);
            Console.WriteLine("Output file: {0}", outputFile);

            if (!File.Exists(inputFile))
            {
                Console.WriteLine("Input file {0} does not exist!", inputFile);                
                return;
            }


            using (StreamWriter writer = new StreamWriter(outputFile))
            { 
                using (StreamReader reader = new StreamReader(inputFile))
                {
                    int lineNo = 1;

                    while (true)
                    {
                        string line = reader.ReadLine();
                        if (line == null) break;

                        string [] parts = line.Split(';');

                        if(parts.Length != 3)
                        {
                            writer.WriteLine("Line #{0} is invalid: \"{1}\"", lineNo, line);
                            continue;
                        }

                        string user = parts[0].Trim();
                        string school = parts[1].Trim();
                        string password = parts[2].Trim();

                        Console.WriteLine("===================================================");
                        Console.WriteLine("Testing line #{3}: user {0}, school {1}, password {2}", user, school, password, lineNo);
                        Console.WriteLine();
                        
                        writer.Write("Testing user {0}, school {1}, password {2}: ", user, school, password);                        

                        try
                        {
                            BackendContext ctx = new BackendContext();
                            ctx.LoginToken = new LoginToken(user, password, school);
                            ctx.Refresh();
                            writer.WriteLine("OK");
                            Console.WriteLine("OK");
                        }
                        catch(Exception ex)
                        {
                            writer.WriteLine("Failed, exception: {0}, message {1}", ex.GetType(), ex.Message);
                            Console.WriteLine("Failed, exception: {0}, message {1}", ex.GetType(), ex.Message);
                        }

                        lineNo++;
                    }

                    Console.WriteLine("Done! Lines processed: {0}", lineNo - 1);
                }
            }

            

            
        }
    }
}
